package com.akk.module.yb.model;

import com.akk.PayConstants;
import com.akk.module.yb.utils.AESUtil;
import com.akk.module.yb.utils.Digest;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;

import java.io.File;
import java.io.FileNotFoundException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 子商户注册请求
 */
public class RegisterRequest {
    private String mainCustomerNumber; //代理商商户编号
    private String mailStr; //电子邮箱
    private String loginPassword; //登录密码
    private String loginPasswordConfirm; //登录密码确认
    private String tradePassword; //交易密码
    private String tradePasswordConfirm; //交易密码确认
    private String requestId; //注册请求号
    private String customerType; //商户类型
    private String businessLicence; //营业执照号
    private String bindMobile; //手机号
    private String signedName; //签约名
    private String certFee;
    private String linkMan; //推荐人
    private String idCard; //身份证号
    private String legalPerson; //法人姓名
    private String minSettleAmount; //起结金额
    private String riskReserveDay; //结算周期
    private String bankAccountType; //银行卡类型
    private String bankAccountNumber; //银行卡号
    private String bankName; //银行卡开户行
    private String accountName; //开户名
    private String areaCode; //地区码
    private String splitter; //分润方
    private String splitterprofitfee; //分润比率
    private String freezeDays; //冻结天数
    private String manualSettle; //是否自助结算
    private String bankCardPhoto; //银行卡正面照
    private String businessLicensePhoto; //个体工商户正面照
    private String idCardPhoto; //身份证正面照
    private String idCardBackPhoto; //身份证背面照
    private String personPhoto; //身份证+银行卡+本人合照
    private String electronicAgreement; //电子协议
    private String hmac; //签名信息
    private String whiteList;

    public String getMainCustomerNumber() {
        return mainCustomerNumber;
    }

    public void setMainCustomerNumber(String mainCustomerNumber) {
        this.mainCustomerNumber = mainCustomerNumber;
    }

    public String getMailStr() {
        return mailStr;
    }

    public void setMailStr(String mailStr) {
        this.mailStr = mailStr;
    }

    public String getLoginPassword() {
        return loginPassword;
    }

    public void setLoginPassword(String loginPassword) {
        this.loginPassword = loginPassword;
    }

    public String getLoginPasswordConfirm() {
        return loginPasswordConfirm;
    }

    public void setLoginPasswordConfirm(String loginPasswordConfirm) {
        this.loginPasswordConfirm = loginPasswordConfirm;
    }

    public String getTradePassword() {
        return tradePassword;
    }

    public void setTradePassword(String tradePassword) {
        this.tradePassword = tradePassword;
    }

    public String getTradePasswordConfirm() {
        return tradePasswordConfirm;
    }

    public void setTradePasswordConfirm(String tradePasswordConfirm) {
        this.tradePasswordConfirm = tradePasswordConfirm;
    }

    public String getRequestId() {
        return requestId;
    }

    public void setRequestId(String requestId) {
        this.requestId = requestId;
    }

    public String getCustomerType() {
        return customerType;
    }

    public void setCustomerType(String customerType) {
        this.customerType = customerType;
    }

    public String getBusinessLicence() {
        return businessLicence;
    }

    public void setBusinessLicence(String businessLicence) {
        this.businessLicence = businessLicence;
    }

    public String getBindMobile() {
        return bindMobile;
    }

    public void setBindMobile(String bindMobile) {
        this.bindMobile = bindMobile;
    }

    public String getSignedName() {
        return signedName;
    }

    public void setSignedName(String signedName) {
        this.signedName = signedName;
    }

    public String getLinkMan() {
        return linkMan;
    }

    public void setLinkMan(String linkMan) {
        this.linkMan = linkMan;
    }

    public String getIdCard() {
        return idCard;
    }

    public void setIdCard(String idCard) {
        this.idCard = idCard;
    }

    public String getLegalPerson() {
        return legalPerson;
    }

    public void setLegalPerson(String legalPerson) {
        this.legalPerson = legalPerson;
    }

    public String getMinSettleAmount() {
        return minSettleAmount;
    }

    public void setMinSettleAmount(String minSettleAmount) {
        this.minSettleAmount = minSettleAmount;
    }

    public String getRiskReserveDay() {
        return riskReserveDay;
    }

    public void setRiskReserveDay(String riskReserveDay) {
        this.riskReserveDay = riskReserveDay;
    }

    public String getBankAccountType() {
        return bankAccountType;
    }

    public void setBankAccountType(String bankAccountType) {
        this.bankAccountType = bankAccountType;
    }

    public String getBankAccountNumber() {
        return bankAccountNumber;
    }

    public void setBankAccountNumber(String bankAccountNumber) {
        this.bankAccountNumber = bankAccountNumber;
    }

    public String getBankName() {
        return bankName;
    }

    public void setBankName(String bankName) {
        this.bankName = bankName;
    }

    public String getAccountName() {
        return accountName;
    }

    public void setAccountName(String accountName) {
        this.accountName = accountName;
    }

    public String getAreaCode() {
        return areaCode;
    }

    public void setAreaCode(String areaCode) {
        this.areaCode = areaCode;
    }

    public String getSplitter() {
        return splitter;
    }

    public void setSplitter(String splitter) {
        this.splitter = splitter;
    }

    public String getSplitterprofitfee() {
        return splitterprofitfee;
    }

    public void setSplitterprofitfee(String splitterprofitfee) {
        this.splitterprofitfee = splitterprofitfee;
    }

    public String getFreezeDays() {
        return freezeDays;
    }

    public void setFreezeDays(String freezeDays) {
        this.freezeDays = freezeDays;
    }

    public String getManualSettle() {
        return manualSettle;
    }

    public void setManualSettle(String manualSettle) {
        this.manualSettle = manualSettle;
    }

    public String getBankCardPhoto() {
        return bankCardPhoto;
    }

    public void setBankCardPhoto(String bankCardPhoto) {
        this.bankCardPhoto = bankCardPhoto;
    }

    public String getBusinessLicensePhoto() {
        return businessLicensePhoto;
    }

    public void setBusinessLicensePhoto(String businessLicensePhoto) {
        this.businessLicensePhoto = businessLicensePhoto;
    }

    public String getIdCardPhoto() {
        return idCardPhoto;
    }

    public void setIdCardPhoto(String idCardPhoto) {
        this.idCardPhoto = idCardPhoto;
    }

    public String getIdCardBackPhoto() {
        return idCardBackPhoto;
    }

    public void setIdCardBackPhoto(String idCardBackPhoto) {
        this.idCardBackPhoto = idCardBackPhoto;
    }

    public String getPersonPhoto() {
        return personPhoto;
    }

    public void setPersonPhoto(String personPhoto) {
        this.personPhoto = personPhoto;
    }

    public String getElectronicAgreement() {
        return electronicAgreement;
    }

    public void setElectronicAgreement(String electronicAgreement) {
        this.electronicAgreement = electronicAgreement;
    }

    public String getHmac() {
        return hmac;
    }

    public void setHmac(String hmac) {
        this.hmac = hmac;
    }


    public static void configFilePart(File f, FilePart fp) {
        String fileName = f.getName();
        fp.setContentType("image/"
                + fileName.substring(fileName.lastIndexOf('.') + 1));
        fp.setCharSet("UTF-8");
    }

    public static void configPdfFilePart(File f, FilePart fp) {
        String fileName = f.getName();
        fp.setContentType("application/"
                + fileName.substring(fileName.lastIndexOf('.') + 1));
        fp.setCharSet("UTF-8");
    }

    public static String signature(RegisterRequest request) {
        String loginPasswordEncrypted = AESUtil.encrypt(request.getLoginPassword(), PayConstants.YB_PARAMS_HMAC_KEY);
        String tradPasswordEncrypted = AESUtil.encrypt(request.getTradePassword(), PayConstants.YB_PARAMS_HMAC_KEY);
        StringBuffer signature = new StringBuffer();
        signature.append(request.getMainCustomerNumber() == null ? ""
                        : request.getMainCustomerNumber())
                .append(loginPasswordEncrypted == null ? ""
                        :loginPasswordEncrypted)
                .append(tradPasswordEncrypted == null ? ""
                        : tradPasswordEncrypted)
                .append(request.getRequestId() == null ? "" : request.getRequestId())
                .append(request.getCustomerType() == null ? "" : request.getCustomerType())
                .append(request.getBusinessLicence() == null ? "" : request.getBusinessLicence())
                .append(request.getBindMobile() == null ? "" : request.getBindMobile())
                .append(request.getSignedName() == null ? "" : request.getSignedName())
                .append(request.getLinkMan() == null ? "" : request.getLinkMan())
                .append(request.getIdCard() == null ? "" : request.getIdCard())
                .append(request.getLegalPerson() == null ? "" : request.getLegalPerson())
                .append(request.getMinSettleAmount() == null ? "" : request.getMinSettleAmount())
                .append(request.getRiskReserveDay() == null ? "" : request.getRiskReserveDay())
                .append(request.getBankAccountNumber() == null ? "" : request.getBankAccountNumber())
                .append(request.getBankName() == null ? "" : request.getBankName())
                .append(request.getAccountName() == null ? "" : request.getAccountName())
                .append(request.getAreaCode() == null ? "" : request.getAreaCode())
                .append(request.getSplitter() == null ? "" : request.getSplitter())
                .append(request.getSplitterprofitfee() == null ? "" : request.getSplitterprofitfee())
                .append(request.getWhiteList() == null?"":request.getWhiteList())
                .append(request.getFreezeDays() == null?"":request.getFreezeDays())
                .append(request.getCertFee() == null ?"":request.getCertFee())
                .append(request.getManualSettle() == null ? "" : request.getManualSettle());
            System.out.println(signature.toString());
            return Digest.hmacSign(signature.toString(), PayConstants.YB_PARAMS_HMAC_KEY);
    }


    public static Part[] builderPart(RegisterRequest request) throws FileNotFoundException {
        List<Part> parts = new ArrayList<Part>();
        parts.add(new StringPart("mainCustomerNumber",request.getMainCustomerNumber()==null?"":request.getMainCustomerNumber(),"UTF-8"));
        parts.add(new StringPart("mailStr",request.getMailStr() == null?"":request.getMailStr(),"UTF-8"));
        parts.add(new StringPart("loginPassword",request.getLoginPassword()==null?"":request.getLoginPassword(),"UTF-8"));
        parts.add(new StringPart("loginPasswordConfirm",request.getLoginPasswordConfirm()==null?"":request.getLoginPasswordConfirm(),"UTF-8"));
        parts.add(new StringPart("tradePassword",request.getTradePassword() == null?"":request.getTradePassword(),"UTF-8"));
        parts.add(new StringPart("tradePasswordConfirm",request.getTradePasswordConfirm()== null?"":request.getTradePasswordConfirm(),"UTF-8"));
        parts.add(new StringPart("requestId",request.getRequestId()==null?"":request.getRequestId(),"UTF-8"));
        parts.add(new StringPart("customerType",request.getCustomerType()==null?"":request.getCustomerType(),"UTF-8"));
        parts.add(new StringPart("businessLicence",request.getBusinessLicence()==null?"":request.getBusinessLicence(),"UTF-8"));
        parts.add(new StringPart("bindMobile",request.getBindMobile()==null?"":request.getBindMobile(),"UTF-8"));
        parts.add(new StringPart("signedName",request.getSignedName()==null?"":request.getSignedName(),"UTF-8"));
        parts.add(new StringPart("linkMan",request.getLinkMan()==null?"":request.getLinkMan(),"UTF-8"));
        parts.add(new StringPart("idCard",request.getIdCard()==null?"":request.getIdCard(),"UTF-8"));
        parts.add(new StringPart("legalPerson",request.getLegalPerson()==null?"":request.getLegalPerson(),"UTF-8"));
        parts.add(new StringPart("minSettleAmount",request.getMinSettleAmount()==null?"":request.getMinSettleAmount(),"UTF-8"));
        parts.add(new StringPart("riskReserveDay",request.getRiskReserveDay()==null?"":request.getRiskReserveDay(),"UTF-8"));
        parts.add(new StringPart("bankAccountNumber",request.getBankAccountNumber()==null?"":request.getBankAccountNumber(),"UTF-8"));
        parts.add(new StringPart("bankName",request.getBankName()==null?"":request.getBankName(),"UTF-8"));
        parts.add(new StringPart("bankAccountType",request.getBankAccountType()==null?"":request.getBankAccountType(),"UTF-8"));
        parts.add(new StringPart("accountName",request.getAccountName()== null?"":request.getAccountName(),"UTF-8"));
        parts.add(new StringPart("areaCode",request.getAreaCode()== null?"":request.getAreaCode(),"UTF-8"));
        File bankCardPhoto = new File(request.getBankCardPhoto());
        FilePart fpBankCardPhoto = new FilePart("bankCardPhoto",bankCardPhoto);
        configFilePart(bankCardPhoto,fpBankCardPhoto);
        parts.add(fpBankCardPhoto);
        File businessLicensePhoto = new File(request.getBusinessLicensePhoto());
        FilePart fpBusinessLicensePhoto = new FilePart("businessLicensePhoto",businessLicensePhoto);
        configFilePart(businessLicensePhoto,fpBusinessLicensePhoto);
        parts.add(fpBusinessLicensePhoto);
        File idCardBackPhoto = new File(request.getIdCardBackPhoto());
        FilePart fpIdCardBackPhoto = new FilePart("idCardBackPhoto",idCardBackPhoto);
        configFilePart(idCardBackPhoto,fpIdCardBackPhoto);
        parts.add(fpIdCardBackPhoto);
        File idCardPhoto = new File(request.getIdCardPhoto());
        FilePart fpIdCardPhoto = new FilePart("idCardPhoto",idCardPhoto);
        configFilePart(idCardPhoto,fpIdCardPhoto);
        parts.add(fpIdCardPhoto);
        File personPhoto = new File(request.getPersonPhoto());
        FilePart fpPersonPhoto = new FilePart("personPhoto",personPhoto);
        configFilePart(personPhoto,fpPersonPhoto);
        parts.add(fpPersonPhoto);
        parts.add(new StringPart("hmac",request.getHmac()== null?"":request.getHmac(),"UTF-8"));
        parts.add(new StringPart("splitter",request.getSplitter() == null?"":request.getSplitter(),"UTF-8"));
        parts.add(new StringPart("splitterprofitfee",request.getSplitterprofitfee()== null?"":request.getSplitterprofitfee(),"UTF-8"));
        parts.add(new StringPart("whiteList",request.getWhiteList()==null?"":request.getWhiteList(),"UTF-8"));
        parts.add(new StringPart("freezeDays",request.getFreezeDays()== null?"":request.getFreezeDays(),"UTF-8"));
        parts.add(new StringPart("certFee",request.getCertFee()==null?"":request.getCertFee(),"UTF-8"));
        parts.add(new StringPart("manualSettle",request.getManualSettle()== null?"":request.getManualSettle(),"UTF-8"));
        parts.add(new StringPart("electronicAgreement",request.getElectronicAgreement()== null?"":request.getElectronicAgreement(),"UTF-8"));

        return parts.toArray(new Part[parts.size()]);

    }


    public String getWhiteList() {
        return whiteList;
    }

    public void setWhiteList(String whiteList) {
        this.whiteList = whiteList;
    }

    public String getCertFee() {
        return certFee;
    }

    public void setCertFee(String certFee) {
        this.certFee = certFee;
    }
}
